package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.BikeHireRestore;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtCardTypeDay;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.SgjjtCityIndicator;
import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.TBusDailyAmount;
import com.supcon.exchangedata.pojo.entity.oracle.*;
import com.supcon.exchangedata.repository.master.*;
import com.supcon.exchangedata.repository.slave4.SlaveGjOperationInfo;
import com.supcon.exchangedata.repository.slave4.SlaveGjOperationInfo1;
import com.supcon.exchangedata.repository.slave4.SlaveGjOperationInfo2;
import com.supcon.exchangedata.repository.slave4.SlaveGjOperationInfo3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 公交运营数据
 *
 * @Author: yangbin
 * @Date: 2022/12/26/9:27
 * @Description:
 */
@Service
@Slf4j
public class GjOperationService {
    @Resource
    private MasterKpiDayRepository masterKpiDayRepository;

    @Resource
    private MasterStatistOtherParametersDayRepository masterStatistOtherParametersDayRepository;

    @Resource
    private MasterStatisticalRealTimeDataRepository masterStatisticalRealTimeDataRepository;
    @Resource
    private MasterStatistOtherParametersHourRepository masterStatistOtherParametersHourRepository;
    @Resource
    private MasterKpiHourRepository kpiHourRepository;

    @Resource
    private SlaveGjOperationInfo slaveGjOperationInfo;
    @Resource
    private SlaveGjOperationInfo1 slaveGjOperationInfo1;

    @Resource
    private SlaveGjOperationInfo2 slaveGjOperationInfo2;
    @Resource
    private SlaveGjOperationInfo3 slaveGjOperationInfo3;
    @Resource
    private RedisTemplate redisTemplate;


    /***********公交日运营数据 start *******************/
    public void gjDailyOperation() {
        List<TBusDailyAmount> topData = slaveGjOperationInfo.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            for (TBusDailyAmount topDatum : topData) {
                LocalDateTime dateTime = topDatum.getDateTime();
                String dataTimeStr = timeFormatter.format(dateTime);
                //运营里程（行驶里程） 10004 10000022153（需要除以10000）
                String operationMileage = topDatum.getOperationMileage();
                if (!StringUtils.isEmpty(operationMileage)) {
                    BigDecimal bigDecimal = new BigDecimal(operationMileage);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022153", dataTimeStr, divide.toPlainString(), "万公里");
                    saveOrUpdateStatistOtherParametersDay(1L, 10004L, dataTimeStr, operationMileage);
                }
                //发车车次 10001
                Integer carAmount = topDatum.getCarAmount();
                if (carAmount != null) {
                    saveOrUpdateStatistOtherParametersDay(1L, 10001L, dataTimeStr, String.valueOf(carAmount));
                }
                //客运量（载客量） 10002 10000022152（需要除以10000）
                String passengerAmount = topDatum.getPassengerAmount();
                if (!StringUtils.isEmpty(passengerAmount)) {
                    BigDecimal bigDecimal = new BigDecimal(passengerAmount);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022152", dataTimeStr, divide.toPlainString(), "万人次");
                    saveRedis(divide.toPlainString(), "万人次", dataTimeStr, "10000022152");
                    saveOrUpdateStatistOtherParametersDay(1L, 10002L, dataTimeStr, passengerAmount);
                }
            }
        }
    }

    public void gjDailyOperationByYear(LocalDateTime startTime, LocalDateTime endTime) {
        List<TBusDailyAmount> topData = slaveGjOperationInfo.findAllByDateTimeBetween(startTime, endTime);
        if (!CollectionUtils.isEmpty(topData)) {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            for (TBusDailyAmount topDatum : topData) {
                LocalDateTime dateTime = topDatum.getDateTime();
                String dataTimeStr = timeFormatter.format(dateTime);
                //运营里程（行驶里程） 10004 10000022153（需要除以10000）
                String operationMileage = topDatum.getOperationMileage();
                if (!StringUtils.isEmpty(operationMileage)) {
                    BigDecimal bigDecimal = new BigDecimal(operationMileage);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022153", dataTimeStr, divide.toPlainString(), "万公里");
                    saveOrUpdateStatistOtherParametersDay(1L, 10004L, dataTimeStr, operationMileage);
                }
                //发车车次 10001
                Integer carAmount = topDatum.getCarAmount();
                if (carAmount != null) {
                    saveOrUpdateStatistOtherParametersDay(1L, 10001L, dataTimeStr, String.valueOf(carAmount));
                }
                //客运量（载客量） 10002 10000022152（需要除以10000）
                String passengerAmount = topDatum.getPassengerAmount();
                if (!StringUtils.isEmpty(passengerAmount)) {
                    BigDecimal bigDecimal = new BigDecimal(passengerAmount);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022152", dataTimeStr, divide.toPlainString(), "万人次");
                    saveOrUpdateStatistOtherParametersDay(1L, 10002L, dataTimeStr, passengerAmount);
                }
            }
        }
    }
    /***********公交日运营数据 end *******************/


    /******公交自行车租还量start ******/
    public void gjBikeOperation() {
        BikeHireRestore bikeHireRestore = slaveGjOperationInfo1.bikeOperation();
        Long hireCount = bikeHireRestore.getHireCount();
        Long restoreCount = bikeHireRestore.getRestoreCount();
        String dataTime = bikeHireRestore.getDataTime();
        if (hireCount != null) {
            //租车量 10006 10000022154
            BigDecimal bigDecimal = new BigDecimal(hireCount);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
            saveOrUpdateKpiDay("10000022154", dataTime, divide.toPlainString(), "万辆次");
            saveOrUpdateStatistOtherParametersDay(1L, 10006L, dataTime, String.valueOf(hireCount));
        }
        if (restoreCount != null) {
            //还车量 10005 10000022355
            BigDecimal bigDecimal = new BigDecimal(restoreCount);
            BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);

            saveOrUpdateKpiDay("10000022355", dataTime, divide.toPlainString(), "万辆次");
            saveOrUpdateStatistOtherParametersDay(1L, 10005L, dataTime, String.valueOf(restoreCount));
        }
    }


    public void gjBikeOperationByYear(LocalDateTime startTime, LocalDateTime endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);
        List<BikeHireRestore> bikeHireRestores = slaveGjOperationInfo1.bikeOperationList(startTimeStr, endTimeStr);

        if (!CollectionUtils.isEmpty(bikeHireRestores)) {
            for (BikeHireRestore bikeHireRestore : bikeHireRestores) {
                Long hireCount = bikeHireRestore.getHireCount();
                Long restoreCount = bikeHireRestore.getRestoreCount();
                String dataTime = bikeHireRestore.getDataTime();
                if (hireCount != null) {
                    //租车量 10006 10000022154
                    BigDecimal bigDecimal = new BigDecimal(hireCount);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022154", dataTime, divide.toPlainString(), "万辆次");
                    saveOrUpdateStatistOtherParametersDay(1L, 10006L, dataTime, String.valueOf(hireCount));
                }
                if (restoreCount != null) {
                    //还车量 10005 10000022355
                    BigDecimal bigDecimal = new BigDecimal(restoreCount);
                    BigDecimal divide = bigDecimal.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
                    saveOrUpdateKpiDay("10000022355", dataTime, divide.toPlainString(), "万辆次");
                    saveOrUpdateStatistOtherParametersDay(1L, 10005L, dataTime, String.valueOf(restoreCount));
                }
            }
        }

    }

    /******公交自行车租还量edn ******/

    /******公交运营实时指数信息 start ******/

    public void busOperation() {
        SgjjtCityIndicator topData = slaveGjOperationInfo2.findTopData();
        if (topData != null) {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime dateTime = topData.getDateTime();
            String dateTimeStr = timeFormatter.format(dateTime);
            Integer hours = topData.getHours();

            //总站点个数
            Integer allStopNum = topData.getAllStopNum();
            //300m覆盖率   10000022858
            String coverage = topData.getCoverage();
            coverage = coverage.replace("%", "");
            //2023-06-28停掉
            //saveRedis(coverage, "%", dateTimeStr, "10000022858");
            //发车间隔 10000022868
            String departureInterval = topData.getDepartureInterval();
            saveRedis(departureInterval, "秒", dateTimeStr, "10000022868");
            saveOrUpdateStatistOtherParametersHour(1L, 10060L, dateTimeStr, departureInterval, hours);
            //首末站点个数
            Integer flStopNum = topData.getFlStopNum();
            saveOrUpdateStatistOtherParametersHour(1L, 10061L, dateTimeStr, String.valueOf(flStopNum), hours);
            //计划出车班次 	10000022855
            Integer planShiftCount = topData.getPlanShiftCount();
            saveRedis(String.valueOf(planShiftCount), "次", dateTimeStr, "10000022855");
            saveOrUpdateStatistOtherParametersHour(1L, 10062L, dateTimeStr, String.valueOf(planShiftCount), hours);
            //全城市正点率 10000022870
            String startPunctualRate = topData.getStartPunctualRate();
            saveRedis(startPunctualRate, "%", dateTimeStr, "10000022870");
            saveOrUpdateStatistOtherParametersHour(1L, 10063L, dateTimeStr, startPunctualRate, hours);
            //全城市准点率 10000022865
            String onTimeRate = topData.getOnTimeRate();
            saveRedis(onTimeRate, "%", dateTimeStr, "10000022865");
            saveOrUpdateStatistOtherParametersHour(1L, 10064L, dateTimeStr, onTimeRate, hours);
            //平均等候时间，单位秒 10000022866
            String waitingTime = topData.getWaitingTime();
            saveRedis(waitingTime, "秒", dateTimeStr, "10000022866");
            saveOrUpdateStatistOtherParametersHour(1L, 10065L, dateTimeStr, waitingTime, hours);

        }
    }

    public void busOperationByTime(LocalDateTime startTime, LocalDateTime endTime) {
        List<SgjjtCityIndicator> sgjjtCityIndicators = slaveGjOperationInfo2.busOperationData(startTime, endTime);

        if (!CollectionUtils.isEmpty(sgjjtCityIndicators)) {
            for (SgjjtCityIndicator topData : sgjjtCityIndicators) {
                if (topData != null) {
                    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDateTime dateTime = topData.getDateTime();
                    String dateTimeStr = timeFormatter.format(dateTime);
                    Integer hours = topData.getHours();
                    //总站点个数
                    Integer allStopNum = topData.getAllStopNum();
                    //300m覆盖率   10000022858
                    String coverage = topData.getCoverage();
                    coverage = coverage.replace("%", "");
                    //发车间隔 10000022868
                    String departureInterval = topData.getDepartureInterval();
                    saveOrUpdateStatistOtherParametersHour(1L, 10060L, dateTimeStr, departureInterval, hours);
                    //首末站点个数
                    Integer flStopNum = topData.getFlStopNum();
                    saveOrUpdateStatistOtherParametersHour(1L, 10061L, dateTimeStr, String.valueOf(flStopNum), hours);
                    //计划出车班次 10000022869
                    Integer planShiftCount = topData.getPlanShiftCount();
                    saveOrUpdateStatistOtherParametersHour(1L, 10062L, dateTimeStr, String.valueOf(planShiftCount), hours);
                    //全城市正点率 10000022870
                    String startPunctualRate = topData.getStartPunctualRate();
                    saveOrUpdateStatistOtherParametersHour(1L, 10063L, dateTimeStr, startPunctualRate, hours);
                    //全城市准点率 10000022865
                    String onTimeRate = topData.getOnTimeRate();
                    saveOrUpdateStatistOtherParametersHour(1L, 10064L, dateTimeStr, onTimeRate, hours);
                    //平均等候时间，单位秒 10000022866
                    String waitingTime = topData.getWaitingTime();
                    saveOrUpdateStatistOtherParametersHour(1L, 10065L, dateTimeStr, waitingTime, hours);

                }
            }
        }

    }

    /******公交运营实时指数信息 end *******/


    /******公交按支付方式客运量 start ******/

    public void passerByPayType() {
        SgjjtCardTypeDay topData = slaveGjOperationInfo3.findTopData();
        if (topData != null) {
            //交易日期 yyyy-MM-dd
            String tradeDate = topData.getTradeDate();
            //银联付
            Integer typeGroup01Sum = topData.getTypeGroup01Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10025L, tradeDate, String.valueOf(typeGroup01Sum));
            //普通卡
            Integer typeGroup02Sum = topData.getTypeGroup02Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10024L, tradeDate, String.valueOf(typeGroup02Sum));
            //免卡
            Integer typeGroup05Sum = topData.getTypeGroup05Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10022L, tradeDate, String.valueOf(typeGroup05Sum));
            //员工卡
            Integer typeGroup06Sum = topData.getTypeGroup06Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10027L, tradeDate, String.valueOf(typeGroup06Sum));


            //其他
            Integer typeGroup07Sum = topData.getTypeGroup07Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10046L, tradeDate, String.valueOf(typeGroup07Sum));
            //支付宝
            Integer typeGroup08Sum = topData.getTypeGroup08Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10047L, tradeDate, String.valueOf(typeGroup08Sum));
            //未知
            Integer typeGroup09Sum = topData.getTypeGroup09Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10048L, tradeDate, String.valueOf(typeGroup09Sum));
            //移动优惠卡
            Integer typeGroup10Sum = topData.getTypeGroup10Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10049L, tradeDate, String.valueOf(typeGroup10Sum));
            //市民卡二维码
            Integer typeGroup11Sum = topData.getTypeGroup11Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10050L, tradeDate, String.valueOf(typeGroup11Sum));
            //农信码
            Integer typeGroup12Sum = topData.getTypeGroup12Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10051L, tradeDate, String.valueOf(typeGroup12Sum));
            //投币
            Integer typeGroup13Sum = topData.getTypeGroup13Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10052L, tradeDate, String.valueOf(typeGroup13Sum));
            //其他2
            Integer typeGroup14Sum = topData.getTypeGroup14Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10053L, tradeDate, String.valueOf(typeGroup14Sum));
            //其他3
            Integer typeGroup15Sum = topData.getTypeGroup15Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10054L, tradeDate, String.valueOf(typeGroup15Sum));
            //其他4
            Integer typeGroup16Sum = topData.getTypeGroup16Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10055L, tradeDate, String.valueOf(typeGroup16Sum));
            //其他5
            Integer typeGroup17Sum = topData.getTypeGroup17Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10056L, tradeDate, String.valueOf(typeGroup17Sum));
            //成人卡
            Integer typeGroup03Sum = topData.getTypeGroup03Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10057L, tradeDate, String.valueOf(typeGroup03Sum));
            //学生卡
            Integer typeGroup04Sum = topData.getTypeGroup04Sum();
            saveOrUpdateStatistOtherParametersDay(1L, 10058L, tradeDate, String.valueOf(typeGroup04Sum));

        }
    }


    public void passerByPayTypeByYear(LocalDateTime startTime, LocalDateTime endTime) {

        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);


        List<SgjjtCardTypeDay> sgjjtCardTypeDays = slaveGjOperationInfo3.busPassengerByPayData(startTimeStr, endTimeStr);
        if (!CollectionUtils.isEmpty(sgjjtCardTypeDays)) {
            for (SgjjtCardTypeDay topData : sgjjtCardTypeDays) {
                if (topData != null) {
                    //交易日期 yyyy-MM-dd
                    String tradeDate = topData.getTradeDate();
                    //银联付
                    Integer typeGroup01Sum = topData.getTypeGroup01Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10025L, tradeDate, String.valueOf(typeGroup01Sum));
                    //普通卡
                    Integer typeGroup02Sum = topData.getTypeGroup02Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10024L, tradeDate, String.valueOf(typeGroup02Sum));
                    //免卡
                    Integer typeGroup05Sum = topData.getTypeGroup05Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10022L, tradeDate, String.valueOf(typeGroup05Sum));
                    //员工卡
                    Integer typeGroup06Sum = topData.getTypeGroup06Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10027L, tradeDate, String.valueOf(typeGroup06Sum));


                    //其他
                    Integer typeGroup07Sum = topData.getTypeGroup07Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10046L, tradeDate, String.valueOf(typeGroup07Sum));
                    //支付宝
                    Integer typeGroup08Sum = topData.getTypeGroup08Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10047L, tradeDate, String.valueOf(typeGroup08Sum));
                    //未知
                    Integer typeGroup09Sum = topData.getTypeGroup09Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10048L, tradeDate, String.valueOf(typeGroup09Sum));
                    //移动优惠卡
                    Integer typeGroup10Sum = topData.getTypeGroup10Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10049L, tradeDate, String.valueOf(typeGroup10Sum));
                    //市民卡二维码
                    Integer typeGroup11Sum = topData.getTypeGroup11Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10050L, tradeDate, String.valueOf(typeGroup11Sum));
                    //农信码
                    Integer typeGroup12Sum = topData.getTypeGroup12Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10051L, tradeDate, String.valueOf(typeGroup12Sum));
                    //投币
                    Integer typeGroup13Sum = topData.getTypeGroup13Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10052L, tradeDate, String.valueOf(typeGroup13Sum));
                    //其他2
                    Integer typeGroup14Sum = topData.getTypeGroup14Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10053L, tradeDate, String.valueOf(typeGroup14Sum));
                    //其他3
                    Integer typeGroup15Sum = topData.getTypeGroup15Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10054L, tradeDate, String.valueOf(typeGroup15Sum));
                    //其他4
                    Integer typeGroup16Sum = topData.getTypeGroup16Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10055L, tradeDate, String.valueOf(typeGroup16Sum));
                    //其他5
                    Integer typeGroup17Sum = topData.getTypeGroup17Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10056L, tradeDate, String.valueOf(typeGroup17Sum));
                    //成人卡
                    Integer typeGroup03Sum = topData.getTypeGroup03Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10057L, tradeDate, String.valueOf(typeGroup03Sum));
                    //学生卡
                    Integer typeGroup04Sum = topData.getTypeGroup04Sum();
                    saveOrUpdateStatistOtherParametersDay(1L, 10058L, tradeDate, String.valueOf(typeGroup04Sum));

                }
            }

        }

    }

    /******公交按支付方式客运量 end *******/


    public void saveRedis(String current, String unit, String dateTimeStr, String code) {
        LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
        map1.put("current", current);
        map1.put("unit", unit);
        map1.put("saveTime", dateTimeStr);
        redisTemplate.opsForValue().set(code, map1);
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateKpiDay(String code, String day, String value, String unit) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        System.out.println(code + "::" + dayOfMonthKey);
        CtKpiDay byCodeAndDayOfMonthKey = masterKpiDayRepository.findByCodeAndDayOfMonthKey(code, dayOfMonthKey);
        if (byCodeAndDayOfMonthKey == null) {
            byCodeAndDayOfMonthKey = new CtKpiDay();
            byCodeAndDayOfMonthKey.setCode(code);
            byCodeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byCodeAndDayOfMonthKey.setLastUpdateTime(LocalDateTime.now());
            byCodeAndDayOfMonthKey.setDataType(code);

        }
        byCodeAndDayOfMonthKey.setUnit(unit);
        byCodeAndDayOfMonthKey.setCurrentValue(value);
        masterKpiDayRepository.save(byCodeAndDayOfMonthKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateKpiHour(String code, LocalDateTime day, String value, Integer hour, String unit) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        CtKpiHour byCodeAndCollectDateAndCollectHour = kpiHourRepository.findByCodeAndCollectDateAndCollectHour(code, day, hour);
        if (byCodeAndCollectDateAndCollectHour == null) {
            byCodeAndCollectDateAndCollectHour = new CtKpiHour();
            byCodeAndCollectDateAndCollectHour.setCode(code);
            byCodeAndCollectDateAndCollectHour.setCollectDate(day);
            byCodeAndCollectDateAndCollectHour.setDataType(code);
            byCodeAndCollectDateAndCollectHour.setUnit(unit);

        }
        byCodeAndCollectDateAndCollectHour.setCurrentValue(value);
        kpiHourRepository.save(byCodeAndCollectDateAndCollectHour);
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStatistOtherParametersHour(Long objId, Long type, String day, String value, Integer hour) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersHour byObjIdAndTypeAndCollectDateAndCollectHour = masterStatistOtherParametersHourRepository.findByObjIdAndTypeAndCollectDateAndCollectHour(objId, type, dayOfMonthKey, hour);
        if (byObjIdAndTypeAndCollectDateAndCollectHour == null) {
            byObjIdAndTypeAndCollectDateAndCollectHour = new StatistOtherParametersHour();
            byObjIdAndTypeAndCollectDateAndCollectHour.setObjId(objId);
            byObjIdAndTypeAndCollectDateAndCollectHour.setType(type);
            byObjIdAndTypeAndCollectDateAndCollectHour.setCollectDate(dayOfMonthKey);
            byObjIdAndTypeAndCollectDateAndCollectHour.setCollectTime(dayOfMonthKey.plusHours(hour));
            byObjIdAndTypeAndCollectDateAndCollectHour.setObjType(1L);
            /* byObjIdAndTypeAndCollectDateAndCollectHour.setLastTime(LocalDateTime.now());*/
            byObjIdAndTypeAndCollectDateAndCollectHour.setInAvgValue(Double.valueOf(value));
            byObjIdAndTypeAndCollectDateAndCollectHour.setCollectHour(hour);
            masterStatistOtherParametersHourRepository.save(byObjIdAndTypeAndCollectDateAndCollectHour);
        } else {
            masterStatistOtherParametersHourRepository.updateHour(Double.valueOf(value), objId, dayOfMonthKey, type, hour);
        }


    }


    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStatistOtherParametersDay(Long objId, Long type, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersDay byObjIdAndTypeAndDayOfMonthKey = masterStatistOtherParametersDayRepository.findByObjIdAndTypeAndDayOfMonthKey(objId, type, dayOfMonthKey);
        if (byObjIdAndTypeAndDayOfMonthKey == null) {
            byObjIdAndTypeAndDayOfMonthKey = new StatistOtherParametersDay();
            byObjIdAndTypeAndDayOfMonthKey.setObjId(objId);
            byObjIdAndTypeAndDayOfMonthKey.setType(type);
            byObjIdAndTypeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byObjIdAndTypeAndDayOfMonthKey.setObjType(1L);
            byObjIdAndTypeAndDayOfMonthKey.setLastTime(LocalDateTime.now());
            byObjIdAndTypeAndDayOfMonthKey.setInAvgValue(Double.valueOf(value));
            masterStatistOtherParametersDayRepository.save(byObjIdAndTypeAndDayOfMonthKey);
        } else {
            masterStatistOtherParametersDayRepository.updateDay(Double.valueOf(value), objId, dayOfMonthKey, type);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void saveRealTimeData(String value, Long objId, Long dataType) {
        BigDecimal targetValue = new BigDecimal(value);
        StatisticalRealTimeData realTimeData = new StatisticalRealTimeData();
        realTimeData.setSumType(1L);
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM");
        LocalDate now = LocalDate.now();
        String format1 = dateTimeFormatter1.format(now);
        realTimeData.setCurMonth(format1);
        realTimeData.setObjId(objId);
        realTimeData.setObjType(1L);
        realTimeData.setDataType(dataType);
        realTimeData.setId(0);
        realTimeData.setCurDate(now.atStartOfDay());
        realTimeData.setCurDateValue(targetValue);
        realTimeData.setLastupdatetime(LocalDateTime.now());
        masterStatisticalRealTimeDataRepository.save(realTimeData);
    }
}
